package com.egao.common.module.system.controller;

import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.system.entity.Layout;
import com.egao.common.module.system.service.LayoutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 网页管理控制层
 * @author pyx
 */
@Api(tags = "网页管理")
@RestController
@RequestMapping("${api}")
public class LayoutController extends BaseController {
    @Autowired
    private LayoutService layoutService;

    @PreAuthorize("hasAuthority('sys:layout:list')")
    @ApiOperation("分页查询网页")
    @ApiPageParam
    @GetMapping("/sys/layout/page")
    public PageResult<Layout> page(HttpServletRequest request){
        PageParam<Layout> pageParam = new PageParam<>(request);
        return new PageResult<>(layoutService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('sys:layout:insert')")
    @ApiOperation("添加网页")
    @OperLog(value = "网页管理", desc = "添加网页", result = true)
    @PostMapping("/sys/layout/insert")
    public JsonResult insert(@RequestBody Layout layout){
        return layoutService.insertLayout(layout);
    }

    @PreAuthorize("hasAuthority('sys:layout:update')")
    @ApiOperation("修改网页")
    @OperLog(value = "网页管理", desc = "修改网页", result = true)
    @PutMapping("/sys/layout/update")
    public JsonResult update(@RequestBody Layout layout){
        return layoutService.updateLayout(layout);
    }

    @PreAuthorize("hasAuthority('sys:layout:delete')")
    @ApiOperation("删除单个网页")
    @OperLog(value = "网页管理", desc = "删除网页", result = true)
    @DeleteMapping("/sys/layout/delete")
    public JsonResult delete(Integer id){
        return layoutService.deleteLayout(id);
    }

    @PreAuthorize("hasAuthority('sys:layout:detail')")
    @ApiOperation("查询单个网页")
    @OperLog(value = "网页管理", desc = "查询单个网页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "网页id", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/sys/layout/get")
    public JsonResult get(Integer id){
        return layoutService.get(id);
    }
}
